<?php
declare (strict_types = 1);
namespace App\Dao;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
/**
 * Dao公用方法
 *
 * @Author czm
 * @DateTime 2020-05-27
 */
trait DaoTrait
{

    protected $driver = ""; //model驱动
    protected $model = ""; //model对象
    //文件位置
    private $fileDir = "/runtime/static/excel";

    /**
     * 根据条件获取对应总数
     * @Author czm
     * @DateTime 2020-05-26
     * @param array $map 
     * @return void
     */
    public function countByWhereTrait($map = [])
    {
        $this->changeModel();
        return $this->model->when(!empty($map), function ($query) use ($map) {
            $query->where($map);
        })->count();
    }

    /**
     * 根据条件判断几率是否存在
     * @Author czm
     * @DateTime 2020-05-26
     * @param array $map
     * @return void
     */
    public function existsByWhereTrait($map = [])
    {
        $this->changeModel();
        return $this->model->when(!empty($map), function ($query) use ($map) {
            $query->where($map);
        })->exists();
    }

    /**
     * 新增内容
     * @Author czm
     * @DateTime 2020-05-27
     * @param [type] $data
     * @return void
     */
    public function addTrait($data)
    {
        $this->changeModel();
        return $this->model->create($data);
    }

    //根据id查找记录
    public function findTrait($id)
    {
        $this->changeModel();
        return $this->model->find($id);
    }

    //批量插入数据
    public function insertTrait($data){
        $this->changeModel();
        return $this->model->insert($data);
    }

    /**
     * 根据id更新内容
     * @Author czm
     * @DateTime 2020-05-27
     * @param [type] $data 
     * @return void
     */
    public function updateTrait($data)
    {
        $this->changeModel();
        $info = $this->findTrait($data['id']);
        if (empty($info)) {
            throw new BusinessException(ErrorCode::ERR_BUESSUS, '检测更新数据为空');
        }
        unset($data['id']);
        return $info->update($data);
    }

    /**
     * 根据条件查询first数据
     * @Author czm
     * @DateTime 2020-05-28
     * @return void
     */
    public function firstByWhereTrait($map){
        $this->changeModel();
        return $this->model->where($map)->first();
    }

    /**
     * 根据条件更新内容
     * @Author czm
     * @DateTime 2020-05-28
     * @param array $where
     * @param array $data
     * @return void
     */
    public function updateByWhereTrait($where=[],$data=[]){
        $this->changeModel();
        return $this->model->where($where)->update($data);
    }



  //生成唯一名称
  protected function getUniqueName()
  {
      return uniqid((string) mt_rand(), false);
  }

  /**
     * 返回文档的相对路径目录
     * @Author czm
     * @DateTime 2020-05-20
     * @return void
     */
    protected function getFileDir()
    {   
        mkdir(BASE_PATH . $this->fileDir, 0777, true);
        return $this->fileDir;
    }

  //删除
  protected function deleteExpiredFile()
  {
      $path = BASE_PATH . $this->getFileDir();
      if (!file_exists($path)) {
          return $this;
      }
      $data = scandir($path);
      foreach ($data as $value) {
          if ($value != '.' && $value != '..') {
              $file = $path . '/' . $value;
              $time = time() - 3600 * 24;
              ($time > fileatime($file)) && unlink($file);
          }
      }
      return $this;
  }


    //切换model实例映射
    public function changeModel()
    {
        switch ($this->driver) {
            case 'App\Dao\RechargeTypeDao':
                $this->model = new \App\Model\RechargeType();
                break;
            case 'App\Dao\RechargeAmountDao':
                $this->model = new \App\Model\RechargeAmount();
                break;
            case 'App\Dao\LotteryPrizeDao':
                $this->model = new \App\Model\LotteryPrize();
                break;
            case 'App\Dao\OilAmountDao':
                $this->model = new \App\Model\OilAmount();
                break;
            case 'App\Dao\CouponBatchDao':
                $this->model = new \App\Model\CouponBatch();
                break;
            case 'App\Dao\CouponDao':
                $this->model = new \App\Model\Coupon();
                break;
            case 'App\Dao\CouponLogDao':
                $this->model = new \App\Model\CouponLog();
                break;
            case 'App\Dao\CouponStatisticsDao':
                $this->model = new \App\Model\CouponStatistics();
                break;
            case 'App\Dao\RechargeAmountChannelDao':
                $this->model = new \App\Model\RechargeAmountChannel();
                break;
            default:
                throw new BusinessException(ErrorCode::ERR_BUESSUS, 'changeModel未注册');

        }
    }

}
